package com;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

public class Function {
	public Function(){}
	private User us;
	private News nw;
	public News getNw() {
		return nw;
	}
	public void setNw(News nw) {
		this.nw = nw;
	}
	public User getUs() {
		return us;
	}

	public void setUs(User us) {
		this.us = us;
	}
	public int admincheck()//管理员登录
	{
		int n=0;//用户名不存在
		DBcon db=new DBcon();
		String sql ="";
		sql="Select *from admin where username="+"'"+us.getUsername()+"'";
		ResultSet rs=db.Query(sql);
		try {
			if(rs.next())
			{
				if(rs.getString(2).equals(us.getPassword()))
				{
					n=1;//登陆成功
				}
				else
				{
					n=2;//密码错误
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}
	public int logcheck()//普通用户登录
	{
		int n=0;//用户名不存在
		DBcon db=new DBcon();
		String sql ="";
		sql="Select *from user where username="+"'"+us.getUsername()+"'";
		ResultSet rs=db.Query(sql);
		try {
			if(rs.next())
			{
				if(rs.getString(2).equals(us.getPassword()))
				{
					n=1;//登陆成功
				}
				else
				{
					n=2;//密码错误
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}
	public int register(User us)//普通用户注册
	{
		int n=0;//用户名已存在
		DBcon db=new DBcon();
		String sql ="";
		sql="Select *from user where username="+"'"+us.getUsername()+"'";
		ResultSet rs=db.Query(sql);
		try {
			if(rs.next())
			{
				return n;//用户名已存在
			}
			else
			{
				db.Register(us.getUsername(), us.getPassword());//注册
				n=1;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}
	public int deluser(User us)//删除单个普通用户
	{
		int n=0;//删除失败
		DBcon db=new DBcon();
		String sql ="";
		sql="delete from user where username="+"'"+us.getUsername()+"'";
		n=db.Update(sql);//删除成功
		return n;
	}
	public int delall()//删除全部普通用户
	{
		int n=0;//删除失败
		DBcon db=new DBcon();
		String sql ="DELETE FROM user;";
		n=db.Update(sql);//删除成功
		return n;
	}
	public int upduser(User us)//修改用户密码
	{
		int n=0;//修改失败
		DBcon db=new DBcon();
		String sql ="";
		sql="UPDATE user SET password="+"'"+us.getPassword()+"'"+" where username="+"'"+us.getUsername()+"'";
		n=db.Update(sql);//修改成功
		return n;
	}
	public int addnews(News nw)//添加新闻
	{
		int n=0;
		DBcon db=new DBcon();
		n=db.Addnews(nw.getTitle(), nw.getContent(), nw.getImage(), nw.getAuthor(), nw.getType(),nw.getKeywords(),nw.getPubtime());
		return n;
	}
	public int updnews(News nw)//更改新闻
	{
		int n=0;
		DBcon db=new DBcon();
		n=db.Updnews(nw.getId(),nw.getTitle(), nw.getContent(), nw.getImage(), nw.getAuthor(), nw.getType(),nw.getKeywords(),nw.getPubtime());
		return n;
	}
	public int delnews(News nw)//删除新闻
	{
		int n=0;
		DBcon db=new DBcon();
		String sql ="";
		sql="delete from news where id="+nw.getId();
		n=db.Update(sql);//删除成功
		return n;
	}
	public News getnew(int id) throws ParseException//获取新闻
	{
		DBcon db=new DBcon();
		News nw=new News();
		String sql ="";
		sql="Select *from news where id="+id;
		ResultSet rs=db.Query(sql);
		try {
			if(rs.next())
			{
				nw.setId(id);
				nw.setTitle(rs.getString(2));
				nw.setContent(rs.getString(3));
				nw.setImage(rs.getString(4));
				nw.setAuthor(rs.getString(5));
				nw.setType(rs.getString(6));
				nw.setKeywords(rs.getString(7));
				java.sql.Timestamp pubtime = rs.getTimestamp(8);
				String pattern = "yyyy-MM-dd HH:mm:ss";
				java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
				String formattedPubtime = sdf.format(pubtime);
				nw.setPubtime(formattedPubtime);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return nw;
	}

}
